// pages/detail/index_detail.js
var postsData = require('../../data/post-data.js')
var call = require("../../utils/request.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curent:0,
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    hidecode: true,
    showcode: false,
    cards: true,
    sid: '',
    card: '获取验证码',
    landing_page: '',
    basepeak: '',
    infotele:'',
    id:'',
    items:[
          {name:'户型详情',id:'list1'},
          {name:'3D全景', id: 'list2' },
          {name:'材料包', id: 'list3' },
    ],
    decorate:[
      {
        types:'瓷砖',
        signs:[
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/1.png'},
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/2.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/1.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/3.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/4.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/5.png' }
        ]
      },
       {
        types: '地板',
         show1: [
           { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/6.png' },
           { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/7.png' },
           { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/8.png' },
           { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/9.png' }
         ]
      },
      {
        types: '卫浴',
        show2: [
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/10.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/11.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/12.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/13.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/14.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/15.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/16.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/17.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/18.png' },
        ]
      },
      {
        types: '开关/吊板',
        show3: [
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/19.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/20.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/21.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/22.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/23.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/24.png' }
        ]
      },
      {
        types: '橱柜/门类',
        show4: [
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/25.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/26.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/27.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/28.png' },
          { img: 'https://img.xtuan.com/xcximgtest/images/decormpany/signs/29.png' }
        ]
      },
    ],
    toView: '',
    now_img: 1,
    background:" http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg",
    clicked:-1,
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var id = e.id;
    var postData = postsData.postlist[id];
    var that = this
    var index = parseInt(e.currentTarget)
    var imgs = postData.imgUrls.length
    var src = postData.imgUrls[0].img
   
    wx.setNavigationBarTitle({
      title: postData.ear//页面标题为路由参数
    })
    that.setData({
      index: index,
      id:id,
      all: imgs,
      background:src,
      postDatas: postData
    })
    call.getData('session', this.shuffleSuc, this.fail);
    setTimeout(function () {
      that.initcode()
    }, 200)
    console.log(postData)
    console.log(imgs)
  },
  //获取session
  shuffleSuc: function (res) {
    console.log(res)
    var that = this;
    var sid = res.data.PHPSESSID
    wx.setStorage({ //存储到本地
      key: "sid",
      data: sid
    })
    that.setData({
      sid: sid
    })

  },
  fail: function () {
    console.log("失败")
  },
  //初始获取验证码
  initcode: function () {
    var that = this
    var sid = that.data.sid
    console.log('奥术大师', sid)
    var text = '获取验证码'
    wx.request({
      url: app.apiUrl("c=zhaobiao/index&a=view_reservations"),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + sid
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data)
        var code = res.data.datas.code
        var baskpage = res.data.datas.landing_page
        that.setData({
          card: code,
          hidecode: false,
          showcode: true,
          basepeak:baskpage
        })
        setTimeout(function () {
          that.setData({
            card: text,
          })
        }, 30000)
      }
    })
  },
  //头部切换图片
  scroll_img:function(e){
    var that =this
    var index = parseInt(e.currentTarget.dataset.index)
    var imgs = that.data.postDatas.imgUrls.length
    var id = that.data.postDatas.imgUrls[index].id;
    var src = that.data.postDatas.imgUrls[index].img
    if (this.data.clicked == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
        clicked: e.currentTarget.id,
        index:index,
        now_img:id,
        all:imgs,
        background: src
    })
    console.log(index)
    console.log(imgs)
  },

  //头部图片滑动
  bindchange: function (e) {
    var that = this
    var index = e.detail.current
    that.setData({
      now_img: e.detail.current + 1
    })
  },
  //锚链接
  jumpTo: function (e) {
    var cur = e.target.dataset.index;
    // 获取标签元素上自定义的 data-opt 属性的值
    let target = e.currentTarget.dataset.skip;
    this.setData({
      clicked: e.currentTarget.id,
      toView: target,
      curent:cur
    })

  },

  //门店列表跳转
  storelist:function(e){
    var id =e.currentTarget.dataset.id
    console.log(id)
      wx.navigateTo({
        url: 'store_list?id='+id,
      })
  },
  //跳转到公司列表
  skipdecompany:function(e){
    var id=e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '../index/decorcompany/decorcompany_detail?id='+id,
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  appointment:function(){
    this.setData({
      showinfo:true,
      mask:true
    })
  },
  hidemask:function(){
    this.setData({
      showinfo: false,
      mask: false
    })
  },
  //输入号码时，预约输入判断
  bindKeyInput: function (e) {
    var value = e.detail.value
    var info = 1
    this.setData({
      info: info,
      hidecode: false,
      showcode: true,
      infotele: value
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //获取验证码
  getCode: function (e) {
    var that = this
    var text = '获取验证码'
    var tele = that.data.infotele
    var card = that.data.card
    var sid = that.data.sid
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (tele == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=get_captcha'),
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "GET",
        success: function (res) {
          console.log(res.data)
          var cards = res.data.datas.code
          //var landing_page = res.data.datas.landing_page
          console.log(cards)
          that.setData({
            card: cards,
            distate: false
          })
          setTimeout(function () {
            that.setData({
              card: text,
              distate: false
            })
          }, 60000)
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        }
      })
    }
  },
  formSubmit: function (e) {
    var that = this
    var mobile = e.detail.value.mobile
    var code = e.detail.value.code
    var id = that.data.tid
    var thecode = that.data.card
    var sid = that.data.sid
    var basepeak = that.data.basepeak
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log('提交', sid)
    console.log(thecode)
    var id = 1
    var title = '预约成功'
    var tsmall = '"恭喜您，预约成功，我们将尽快与您联系!'
    if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '手机号码不能为空！'
      })
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    } else if (code == '') {
      that.setData({
        alertmess: true,
        message: '验证码不能为空！'
      })
      that.settime()
    } else if (code != thecode) {
      that.setData({
        alertmess: true,
        message: '验证码不对，请重新输入！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_reservations'),
        data: {
          phone: mobile,
          code: code,
          company_id: id,
          landing_page: basepeak
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "POST",
        success: function (res) {
          console.log(res.data)
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx.navigateTo({
              url: '../index/free/applysuccess?data=' + [id, title, tsmall],
            })
          } else {
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        }
      })
      this.setData({
        showModal: false,
        Modalcontent: false,
        showinfo: false,
        mask: false
      })
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  }

 
})